<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\index\controller;
use app\admin\logic\Log;
use app\common\model\PeopleRegister;
use app\common\model\TypeMajor;
use Elastica\Client;
use Elastica\Query\BoolQuery;
use Elastica\QueryBuilder;
use Elastica\Search;
use Elastica\Type\Mapping;
use think\Cache;
use think\Db;
use think\db\Query;
use think\Debug;

/**
 * 前端首页控制器
 */
class ProjectData extends IndexBase
{

    public $client ;
    public $search ;
    public $zhj_index;

    static $tables = [
        'project_new'=>"id,project_name,project_com_num,project_num,project_province_num,project_area,project_unit,project_type,project_nature,project_use,project_allmoney,project_acreage,project_level,project_wnum,company_url,project_url,status,prjsize,ismajor,alllength,datalevel,FROM_UNIXTIME((begindate/1000), '%Y-%m-%d' ) AS begindate,FROM_UNIXTIME((endate/1000), '%Y-%m-%d' ) AS endate",
        'project_bid'=>"id,pro_num,bid_type as type,bid_way,bid_unitname,FROM_UNIXTIME((bid_date/1000), '%Y-%m-%d' ) AS bid_date,bid_money as money,bid_area,bid_pnum,pro_bid_num,company_url,bid_url,kid,data_level,daili_com_url",
        'project_contract'=>"id,pro_num,contract_url,pro_contract_num,contract_type as type,contract_denum,contract_money as money,contract_fabao_name,fabao_url,chengbao_name,chengbao_url,data_level,contract_bianhao,FROM_UNIXTIME((contractdate/1000), '%Y-%m-%d' ) AS contractdate",
        'project_finish'=>"id,pro_num,pro_fin_num,fin_money as money,fin_area,FROM_UNIXTIME((end_time/1000), '%Y-%m-%d' ) AS end_time,fin_denum,fin_pass_num,finish_url,data_level,pro_re_type,pro_size,FROM_UNIXTIME((bdate/1000), '%Y-%m-%d' ) AS bdate,span,length,corprole as type,company_name,company_url,per_juese,per_name,pro_fi_num,finish_id",
        'project_permit'=>"id,pro_per_num,per_money as money,per_area,FROM_UNIXTIME((pass_time/1000), '%Y-%m-%d' ) AS pass_time,per_denum,data_level,pro_num,per_url,corprole as type,company_name,company_url,per_juese,per_url_ext,per_name,pro_fi_num,permit_id"
    ];

    public function __construct()
    {
        vendor('zhj_es.Index');
        vendor('zhj_es.jianzhu.Index');
        vendor('zhj_es.jianzhu.ConstName');
        $config = $_config = [
            'host' => '47.92.205.189',
            'port' => '9920',
        ];
        $this->client = new Client($config);
        $this->zhj_index = new \zhj_es\Index($this->client);
        $this->jianzhu = new \zhj_es\jianzhu\Index($this->client);
    }


    public function createIndex()
    {
        $elasticIndex = new \zhj_es\Index($this->client);
        dump($elasticIndex->createIndex());
    }
    //导入数据
    public function importData()
    {

        ini_set("memory_limit", "1024M");
        ini_set("max_execution_time", 0);
        set_time_limit(0);
        $elasticIndex = new \zhj_es\Index($this->client);

        $count =  Db::name('project_new')->distinct(true)->count('id');
        $size = 1500;
        $page = ceil($count/$size);
        $k = 0;
        while($page>=0){
            $companyUrlList = Db::name('project_new')->field(self::$tables['project_new'])->distinct(true)->order('id')->limit($k*$size,$size)->select();

            foreach ($companyUrlList as $k1=>&$v){
                $v['project_bid'] = db('project_bid')->where(['pro_num'=>$v['project_num']])->field(self::$tables['project_bid'])->select();
                if(!empty($v['project_bid'])){
                    foreach ($v['project_bid'] as &$v1){
                        if(empty($v1['money'])  || $v1['money']=='None'){
                            $v1['money'] = 0;
                        }
                    }
                }
                $v['project_contract'] = db('project_contract')->where(['pro_num'=>$v['project_num']])->field(self::$tables['project_contract'])->select();
                if(!empty($v['project_contract'])){
                    foreach ($v['project_contract'] as &$v2){
                        if(empty($v2['money'])  || $v2['money']=='None'){
                            $v2['money'] = 0;
                        }
                    }
                }
                $v['project_permit'] = db('project_permit_merge')->where(['pro_num'=>$v['project_num']])->field(self::$tables['project_permit'])->select();
                if(!empty($v['project_permit'])){
                    foreach ($v['project_permit'] as &$v3){
                        if(empty($v3['money'])  || $v3['money']=='None'){
                            $v3['money'] = 0;
                        }
                    }
                }
                $v['project_finish'] = db('project_finish_merge')->where(['pro_num'=>$v['project_num']])->field(self::$tables['project_finish'])->select();
                if(!empty($v['project_finish'])){
                    foreach ($v['project_finish'] as &$v4){
                        if(empty($v4['money'])  || $v4['money']=='None'){
                            $v4['money'] = 0;
                        }
                    }
                }
            }

            $elasticIndex->importProjectData($companyUrlList);
            $page--;
            $k++;
            echo $page."\r\n";
        }
        //$list = $this->improtDataNew();
        //$elasticIndex->importData($list);
        \think\Log::error("run 1 page");
        //  (new Log())->logAdd('import_data',"run $page page");
    }
    public function importDataV2()
    {

        ini_set("memory_limit", "2048M");
        ini_set("max_execution_time", 0);
        set_time_limit(0);
        $elasticIndex = new \zhj_es\Index($this->client);

        $key = 'count_key1221';
        $count = Cache::get($key);
        if(!$count){
            $count =  Db::name('project_new')->count('project_num');
            Cache::set($key,$count,60*60*24);
        }
        $size = 1500;
        $page =ceil($count/$size);
        $k = 0;

        while($page>=0){
            echo 'time__1__'.Debug::getUseTime()."\r\n";
                $companyUrlList = Db::name('project_new')->field(self::$tables['project_new'])->limit($k*$size,$size)->select();
            echo 'time__2__'.Debug::getUseTime()."\r\n";
                $companyUrlList = array_column($companyUrlList,NULL,'project_num');

                $pro_nums = array_column($companyUrlList, 'project_num');
                $bidLists  =  db('project_bid')->where('pro_num','in',$pro_nums)->field(self::$tables['project_bid'])->select();
                //echo 'time__1__'.Debug::getUseTime()."\r\n";
                foreach ($companyUrlList as &$v1){
                    $v1['project_bid'] = [];
                    $v1['project_contract'] = [];
                    $v1['project_permit'] = [];
                    $v1['project_finish'] = [];
                }
                if(!empty($bidLists)){
                    foreach ($bidLists as $k1=>$v){
                        if(empty($v['money'])  || $v['money']=='None'){
                            $v['money'] = 0;
                        }
                        if($v['type']=='监理'){
                            $v['new_type'] = '监理';
                        }elseif($v['type']=='施工' || $v['type']=='工程总承包'){
                            $v['new_type'] = '施工';
                        }elseif($v['type']=='设计'){
                            $v['new_type'] = '设计';
                        }elseif($v['type']=='勘察'){
                            $v['new_type'] = '勘察';
                        }else{
                            $v['new_type'] = '其他';
                        }
                        if(empty($v['bid_money'])  || $v['bid_money']=='None'){
                            $v['bid_money'] = 0;
                        }
                        if(!empty($companyUrlList[$v['pro_num']])){
                            $companyUrlList[$v['pro_num']]['project_bid'][] = $v;
                        }
                    }
                }
            //echo 'time__2__'.Debug::getUseTime()."\r\n";
                $contractLists = db('project_contract')->where('pro_num','in',$pro_nums)->field(self::$tables['project_contract'])->select();
            //echo 'time__3__'.Debug::getUseTime()."\r\n";
                if(!empty($contractLists)){
                    foreach ($contractLists as $k1=>$v){

                        if($v['type']=='监理'){
                            $v['new_type'] = '监理';
                        }elseif($v['type']=='设计'){
                            $v['new_type'] = '设计';
                        }elseif($v['type']=='勘察'){
                            $v['new_type'] = '勘察';
                        }elseif(in_array($v['type'],['施工总包','施工分包','工程总承包','劳务施工'])){
                            $v['new_type'] = '施工';
                        }else{
                            $v['new_type'] = '其他';
                        }

                        if(empty($v['money'])  || $v['money']=='None'){
                            $v['money'] = 0;
                        }
                        if(!empty($companyUrlList[$v['pro_num']])){
                            $companyUrlList[$v['pro_num']]['project_contract'][] = $v;
                        }
                    }
                }
            echo 'time__4__'.Debug::getUseTime()."\r\n";
                $permitLists = db('project_permit_merge')->where('pro_num','in',$pro_nums)->field(self::$tables['project_permit'])->select();
            echo 'time__5__'.Debug::getUseTime()."\r\n";
                if($permitLists){
                    foreach ($permitLists as $k1=>$v){
                        if(empty($v['money'])  || $v['money']=='None'){
                            $v['money'] = 0;
                        }
                        if($v['type']=='监理企业'){
                            $v['new_type'] = '监理';
                        }elseif($v['type']=='施工企业' || $v['type']=='工程总承包单位'){
                            $v['new_type'] = '施工';
                        }elseif($v['type']=='设计企业'){
                            $v['new_type'] = '设计';
                        }elseif($v['type']=='勘察企业'){
                            $v['new_type'] = '勘察';
                        }else{
                            $v['new_type'] = '其他';
                        }
                        if(!empty($companyUrlList[$v['pro_num']])){
                            $companyUrlList[$v['pro_num']]['project_permit'][] = $v;
                        }

                    }
                }
            echo 'time__6__'.Debug::getUseTime()."\r\n";

                $finishLists= db('project_finish_merge')->where('pro_num','in',$pro_nums)->field(self::$tables['project_finish'])->select();
            echo 'time__7'.Debug::getUseTime()."\r\n";
                if($finishLists){
                    foreach ($finishLists as $k1=>$v){
                        if(empty($v['money'])  || $v['money']=='None'){
                            $v['money'] = 0;
                        }
                        if($v['type']=='监理企业'){
                            $v['new_type'] = '监理';
                        }elseif($v['type']=='施工企业'){
                            $v['new_type'] = '施工';
                        }elseif($v['type']=='设计企业'){
                            $v['new_type'] = '设计';
                        }elseif($v['type']=='勘察企业'){
                            $v['new_type'] = '勘察';
                        }else{
                            $v['new_type'] = '其他';
                        }
                        if(!empty($companyUrlList[$v['pro_num']])){
                            $companyUrlList[$v['pro_num']]['project_finish'][] = $v;
                        }


                    }
                }
            echo 'time__8__'.Debug::getUseTime()."\r\n";
            $elasticIndex->importProjectData($companyUrlList);

            $page--;
            $k++;
            echo $page.'----'.$k.'---'.Debug::getUseTime()."\r\n";
            }

        //$list = $this->importDataNew();
        //$elasticIndex->importData($list);

        //  (new Log())->logAdd('import_data',"run $page page");
    }
    public function deleteIndex()
    {
        $index =  'jz_people_register';
        $res =  $this->zhj_index->deleteIndex($index);
        var_dump($res);exit;
    }




    /**
     * 检查数据
     */
    public function checkPronumData()
    {
        ini_set("memory_limit", "1024M");
        ini_set("max_execution_time", 0);
        set_time_limit(0);
        $elasticIndex = new \zhj_es\Index($this->client);

        $count =  Db::name('project_new')->distinct(true)->count('id');

        $size = 1500;
        $page = ceil($count/$size);
        $k = 0;
        $elasticType = (new Search($this->client))->addIndex('jz_project_merge_new')->addType('_doc');
        while($page>=0) {
            echo '----start---'.Debug::getUseTime()."\r\n";
            $companyUrlList = Db::name('project_new')->field(self::$tables['project_new'])->distinct(true)->order('id')->field('project_num')->limit($k * $size, $size)->select();
            foreach ($companyUrlList as $k1=>$v){
                $query = new \Elastica\Query();
                $qb = new BoolQuery();
                $term = new QueryBuilder();
                $qb->addMust($term->query()->term(['project_num'=>$v['project_num']]));
                $query->setQuery($qb);
                $resultSet = $elasticType->search($query);
                $count =$resultSet->getTotalHits();
                if($count>0){
                    //echo "$v[project_num]\r\n";
                    continue;
                }else{
                    echo "---$v[project_num]--->无\r\n";
                }
            }
            $page--;
            $k++;
            echo $page.'----'.$k.'---'.Debug::getUseTime()."\r\n";


    }}

}
